starter.controller('bookingPlanController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page,localStore,$location,$filter,$ionicHistory,sessionStore) {
	
		$scope.line = $state.params.line || sessionStore.getObject('booking.line') || {};
		$scope.order = {
				lineId : $scope.line.lineId || '',
				plan: {}
		};
		
		$scope.today = new Date();
		$scope.startDate = $filter('date')($scope.today, 'yyyy-MM-dd');
		
		$scope.times = [];
		$($scope.line.plans).each(function(idx, plan){ 
			if(idx == 0){
				$scope.order.plan = plan;
			}
			$scope.times.push(
				{
					id: plan.planId,
					value: plan.departTime,
				}
			);
		});
		
		$scope.selectTime = function(){
			if($scope.times.length == 0){
				return;
			}
			
			var lineSelect = new IosSelect(1, 
		            [$scope.times],
		            {
		                container: '.optionsSelectLineContainer',
		                title: '选择乘车时间',
		                itemHeight: 40,
		                itemShowCount: 5,
		                oneLevelId:$scope.order.plan.planId || '',
		                callback: function (selectOneObj) {
							$scope.$apply(function () {
								$($scope.line.plans).each(function(idx, _plan){ 
									if(_plan.planId == selectOneObj.id){
										$scope.order.plan = _plan;
										return false;
									}
								});
						    });
		                }
		        });
		};
		
		$scope.next = function(){
			if(!$scope.order.plan.planId || $scope.order.plan.planId == ''){
				Page.alert("请选择乘车时间!");
				return;
			}
			sessionStore.setObject('booking.order', $scope.order);
			$state.go('booking_date', {line: $scope.line, order: $scope.order}, {reload:true});
		};
		
		require.config({
		    paths: {
		        "amap": "http://webapi.amap.com/maps?v=1.3&key=470035538be50c5cf7080a0aada29aad"
		    }
		});
		
		require(['amap'], function() {
			$scope.lineStop = {
					mapContainer : 'lineMap',
					defaultWeidu : 30.595444,
		            defaultJingdu : 114.296308,
					init : function(stops){
						$("#lineMap").empty();
						$scope.lineStop.map = new AMap.Map(this.mapContainer, {
		                    resizeEnable: true,
		                    center: [this.defaultJingdu,this.defaultWeidu],
		                    features : ['bg','road']
		                });
						$scope.lineStop.path = [];
						$(stops).each(function(idx, stop) {
		                    if(stop.weidu && stop.jingdu){
		                        $scope.lineStop.path.push([stop.jingdu,stop.weidu]);
		                    }
		                });
						
						var section = {};
						AMap.service(["AMap.PolyEditor"], function() {
		                    section.line =(function(){
		                        return new AMap.Polyline({
		                            map: $scope.lineStop.map,
		                            path: $scope.lineStop.path,
		                            strokeColor: "#FF33FF",//线颜色
		                            strokeOpacity: 1,//线透明度
		                            strokeWeight: 3,//线宽
		                            strokeStyle: "solid",//线样式
		                            showDir:true,
		                            lineJoin:'round'
		                        });
		                    })();
		                });
						$(stops).each(function(idx, stop) {
		                    if(stop.weidu && stop.jingdu && stop.stopType != '0'){
		                        $scope.lineStop.createStopMarker(stop);
		                    }
		                });
						 $scope.lineStop.map.setFitView();
					},
					createStopMarker : function(stop){
//						var content = '<img src="./img/icon/dd-via.png" style="width:8px;height:8px;"></img>'
//						 + '<div style="background-color:#ff0000;color:#fff;width:12px;font-size:12px;line-height:14px;">' + stop.stopName + '</div>';
//		                 return new AMap.Marker({
//		                        position: [stop.jingdu,stop.weidu],
//		                        map: $scope.lineStop.map,
//		                        offset : new AMap.Pixel(-4,-10),
//		                        zIndex: 1100,
//		                        content : content
//		                 });
		                 var content = '<img src="./img/icon/' + ( stop.stopType == '1' ? 'up_icon.png' : 'down_icon.png' ) + '" style="width:18px;height:30px;"></img>'
							 + '<div style="font-weight:bold;color:#ff0000;width:18px;font-size:12px;line-height:14px;text-align:center;">' + stop.stopName + '</div>';
			                 return new AMap.Marker({
			                        position: [stop.jingdu,stop.weidu],
			                        map: $scope.lineStop.map,
			                        offset : new AMap.Pixel(-9,-30),
			                        zIndex: 1100,
			                        content : content
			                 });
		           	}
				};
			$scope.lineStop.init($scope.line.stops);
		});
		
		
		
		
		
});